package com.tjs.lucky.infrastructure.repository;

import com.tjs.lucky.domain.rule.mdel.aggregates.TreeRuleRich;
import com.tjs.lucky.domain.rule.mdel.vo.TreeNodeLineVO;
import com.tjs.lucky.domain.rule.mdel.vo.TreeNodeVO;
import com.tjs.lucky.domain.rule.mdel.vo.TreeRootVO;
import com.tjs.lucky.domain.rule.repository.IRuleRepository;
import com.tjs.lucky.infrastructure.mapper.RuleTreeMapper;
import com.tjs.lucky.infrastructure.mapper.RuleTreeNodeLineMapper;
import com.tjs.lucky.infrastructure.mapper.RuleTreeNodeMapper;
import com.tjs.lucky.infrastructure.po.RuleTree;
import com.tjs.lucky.infrastructure.po.RuleTreeNode;
import com.tjs.lucky.infrastructure.po.RuleTreeNodeLine;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 田佳澍
 * @Description: 规则信息仓储服务
 * @DateTime: 2023/5/29 15:47
 **/
@Repository
public class RuleRepository implements IRuleRepository {

    @Resource
    private RuleTreeMapper ruleTreeMapper;
    @Resource
    private RuleTreeNodeMapper ruleTreeNodeMapper;
    @Resource
    private RuleTreeNodeLineMapper ruleTreeNodeLineMapper;


    @Override
    public TreeRuleRich queryTreeRuleRich(Long treeId) {
        //根据treeId查询规则树
        RuleTree ruleTree = ruleTreeMapper.queryRuleTreeByTreeId(treeId);

        //封装TreeRootVO
        TreeRootVO treeRoot = new TreeRootVO();
        treeRoot.setTreeId(ruleTree.getId());
        treeRoot.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
        treeRoot.setTreeName(ruleTree.getTreeName());

        // 树节点->树连接线
        Map<Long, TreeNodeVO> treeNodeMap = new HashMap<>();
        //1、查询该规则树下的所有节点
        List<RuleTreeNode> ruleTreeNodes = ruleTreeNodeMapper.queryRuleTreeNodeList(treeId);
        for (RuleTreeNode treeNode : ruleTreeNodes) {

            //根据节点 查询该节点下 的节点连线集合
            List<RuleTreeNodeLine> ruleTreeNodeLineList = ruleTreeNodeLineMapper.queryRuleTreeNodeLineList(treeId, treeNode.getId());

            //转换为VO集合
            List<TreeNodeLineVO> treeNodeLineInfoList = ruleTreeNodeLineList.stream().map(nodeLine -> {
                TreeNodeLineVO treeNodeLineInfo = new TreeNodeLineVO();
                treeNodeLineInfo.setNodeIdFrom(nodeLine.getNodeIdFrom());
                treeNodeLineInfo.setNodeIdTo(nodeLine.getNodeIdTo());
                treeNodeLineInfo.setRuleLimitType(nodeLine.getRuleLimitType());
                treeNodeLineInfo.setRuleLimitValue(nodeLine.getRuleLimitValue());
                return treeNodeLineInfo;
            }).collect(Collectors.toList());

            //RuleTreeNode ==> TreeNodeVO
            TreeNodeVO treeNodeInfo = new TreeNodeVO();
            treeNodeInfo.setTreeId(treeNode.getTreeId());
            treeNodeInfo.setTreeNodeId(treeNode.getId());
            treeNodeInfo.setNodeType(treeNode.getNodeType());
            treeNodeInfo.setNodeValue(treeNode.getNodeValue());
            treeNodeInfo.setRuleKey(treeNode.getRuleKey());
            treeNodeInfo.setRuleDesc(treeNode.getRuleDesc());
            treeNodeInfo.setTreeNodeLineChildList(treeNodeLineInfoList);

            treeNodeMap.put(treeNode.getId(), treeNodeInfo);
        }

        return new TreeRuleRich(treeRoot, treeNodeMap);
    }
}
